package com.waijiaojun.tpo.utils;

import com.waijiaojun.tpo.service.account.ShiroDbRealm;
import com.waijiaojun.tpo.web.PaymentController;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Peter on 2018/1/29.
 */
public class UserUtil {
    private static Logger logger = LoggerFactory.getLogger(UserUtil.class);

    public static ShiroDbRealm.ShiroUser getCurrentUser() {
        ShiroDbRealm.ShiroUser user = null;
        if (Constants.testUserModel) {
            user = mockUser();
        } else {
            Subject currentUser = SecurityUtils.getSubject();
            user = (ShiroDbRealm.ShiroUser) currentUser.getPrincipal();

        }
        logger.info("current User -->> " + JsonUtil.toJson(user));
        return user;
    }

    public static String getCurrentUserOpenId() {
        if (getCurrentUser() != null) {
            return getCurrentUser().getOpenid();
        } else {
            return null;
        }
    }

    private static ShiroDbRealm.ShiroUser mockUser() {
        ShiroDbRealm.ShiroUser user = new ShiroDbRealm.ShiroUser(Constants.testUserOpenid, Constants.testUserNickname);
        return user;
    }
}
